package com.tuju.jetpackfirstdemo.ui.img_down;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.liulishuo.okdownload.DownloadContext;
import com.liulishuo.okdownload.DownloadListener;
import com.liulishuo.okdownload.DownloadTask;
import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo;
import com.liulishuo.okdownload.core.cause.EndCause;
import com.liulishuo.okdownload.core.cause.ResumeFailedCause;
import com.liulishuo.okdownload.core.listener.DownloadListener1;
import com.liulishuo.okdownload.core.listener.assist.Listener1Assist;
import com.tuju.jetpackfirstdemo.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ImgDownActivity extends AppCompatActivity {

    private Button btn_downing;

    private List<String> mImgLists = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_img_down);
        mImgLists.add("https://his.ipalmap.com/screen/doctor/0A6S01/丁秀梅.jpg");
        mImgLists.add("https://his.ipalmap.com/screen/doctor/0A6S01/超声科 郭玉峰.jpg");
        mImgLists.add("https://his.ipalmap.com/screen/doctor/0A6S01/超声科 牟书堂.jpg");
        mImgLists.add("https://his.ipalmap.com/screen/doctor/0A6S01/超声科 符兴江.jpg");
        initView();
    }

    private void initView() {
        btn_downing = findViewById(R.id.btn_downing);
        btn_downing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < mImgLists.size(); i++) {
                    final int finalI = i;
                    String name = onDownLoad(mImgLists.get(i), new ImageDownLoadCallBack() {
                        @Override
                        public void onDownLoadSuccess(File file) {

                        }

                        @Override
                        public void onDownLoadSuccess(Bitmap bitmap) {

                        }

                        @Override
                        public void onDownLoadFailed() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(ImgDownActivity.this, mImgLists.get(finalI) + "下载失败!", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    });
                    sb.append(name);
                    sb.append("\n");
                }
                Toast.makeText(ImgDownActivity.this, "下载完成：" + sb.toString(), Toast.LENGTH_SHORT).show();
            }
        });

        Button btn_downing_ok = findViewById(R.id.btn_downing_ok);
        btn_downing_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tagerPath = FilePathManager.getImagePath();
                String filename = DownLoadImageService.imageStr(mImgLists.get(0));
                DownloadTask task = new DownloadTask.Builder(mImgLists.get(0), tagerPath,filename)
                        .setFilename(filename)
                        // the minimal interval millisecond for callback progress
                        .setMinIntervalMillisCallbackProcess(30)
                        // do re-download even if the task has already been completed in the past.
                        .setPassIfAlreadyCompleted(false)
                        .build();

                DownloadListener1 listener = new DownloadListener1() {
                    @Override
                    public void taskStart(@NonNull DownloadTask task, @NonNull Listener1Assist.Listener1Model model) {

                    }

                    @Override
                    public void retry(@NonNull DownloadTask task, @NonNull ResumeFailedCause cause) {

                    }

                    @Override
                    public void connected(@NonNull DownloadTask task, int blockCount, long currentOffset, long totalLength) {

                    }

                    @Override
                    public void progress(@NonNull DownloadTask task, long currentOffset, long totalLength) {
                        Log.i("yanze_image_progress", "progress: " + currentOffset + " / " + totalLength);
                    }

                    @Override
                    public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause, @NonNull Listener1Assist.Listener1Model model) {

                    }
                };

                task.enqueue(listener);

                // cancel
//                task.cancel();

                // execute task synchronized
//                task.execute(listener);
            }
        });
    }

    /**
     * 启动图片下载线程
     */
    protected String onDownLoad(String url, ImageDownLoadCallBack callBacks) {
        if (TextUtils.isEmpty(url)) {
            return "";
        }
        final String tageUrl = DownLoadImageService.imageStr(url);
        DownLoadImageService service = new DownLoadImageService(this, url, callBacks);
        //启动图片下载线程
        new Thread(service).start();
        return FilePathManager.getImagePath() + tageUrl;
    }
}
